<?php
class demands_model extends CRM_Model{
	private $typeDocRid;
	public function __construct(){
		parent::__construct();
		$typeDocRow = $this->GetTypeDoc();
		$this->typeDocRid = $typeDocRow->rid; 
		$this->ciObject->load->model('outgoingincoming_model');
	}
	
	public function GetDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_demands_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_demands_headers._demandsstatuses_rid as _demandsstatuses_rid,
							_demandsstatuses.demandsstatuses_name as demandsstatuses_name,
							_demands_headers._currencies_rid as _currencies_rid,
							_currencies.code as currency_code,
							_demands_headers.agreement as agreement,
							_demands_headers.summ as summ,
							_demands_headers.summ_text as summ_text,
							_demands_headers.summ_pay as summ_pay,
							count(_demands_rows.rid) as clients_quan,
							_tours._food_rid as _food_rid,
							_food.food_name as food_name,
							_tours._touroperators_rid as _touroperators_rid,
							_touroperators.touroperator_name as touroperator_name,
							_tours._rooms_rid as _rooms_rid,
							_rooms.room_name as room_name,
							_tours._countries_rid as _countries_rid,
							_countries.country_name as country_name,
							_tours._curourts_rid as _curourts_rid,
							_curourts.curourt_name as curourt_name,
							_tours._hotelscats_rid as _hotelscats_rid,
							_hotelscats.hotelcat_name as hotelcat_name,
							_tours.hotel_name as hotel_name,
							DATEDIFF(_tours.date_to, _tours.date_from) as tour_period,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_demands_headers', '_demands_headers._documents_rid = _documents.rid');
		$this->db->join('_demands_rows', '_demands_rows._demands_headers_rid = _demands_headers.rid');
		$this->db->join('_demandsstatuses', '_demands_headers._demandsstatuses_rid = _demandsstatuses.rid');
		$this->db->join('_currencies', '_demands_headers._currencies_rid = _currencies.rid');
		$this->db->join('_tours', '_demands_headers._tours_rid = _tours.rid');
		$this->db->join('_food', '_tours._food_rid = _food.rid');
		$this->db->join('_touroperators', '_tours._touroperators_rid = _touroperators.rid');
		$this->db->join('_rooms', '_tours._rooms_rid = _rooms.rid');
		$this->db->join('_countries', '_tours._countries_rid = _countries.rid');
		$this->db->join('_curourts', '_tours._curourts_rid = _curourts.rid');
		$this->db->join('_hotelscats', '_tours._hotelscats_rid = _hotelscats.rid');
		$this->db->join('_clients', '_demands_rows._clients_rid = _clients.rid');
		$this->db->where(array('_documents._documents_types_rid'=>$this->typeDocRid));
		$this->db->groupby('_demands_rows._demands_headers_rid');
		if($searchRule = $this->ciObject->GetSessionData('searchrule')){
			$whereArr = array();
			if(isset($searchRule['_demands_headers.date_doc>='])) { $whereArr['_demands_headers.date_doc>='] = $this->toMySQLDate($searchRule['_demands_headers.date_doc>=']); unset($searchRule['_demands_headers.date_doc>=']); } 
			if(isset($searchRule['_demands_headers.date_doc<='])) { $whereArr['_demands_headers.date_doc<='] = $this->toMySQLDate($searchRule['_demands_headers.date_doc<=']); unset($searchRule['_demands_headers.date_doc<=']); } 
			if(isset($searchRule['_demands_headers.summ<='])) { $whereArr['_demands_headers.summ<='] = $searchRule['_demands_headers.summ<=']; unset($searchRule['_demands_headers.summ<=']); } 
			if(isset($searchRule['_demands_headers.summ>='])) { $whereArr['_demands_headers.summ>='] = $searchRule['_demands_headers.summ>=']; unset($searchRule['_demands_headers.summ>=']); } 
			if(isset($searchRule['_demands_headers.summ_pay<='])) { $whereArr['_demands_headers.summ_pay<='] = $searchRule['_demands_headers.summ_pay<=']; unset($searchRule['_demands_headers.summ_pay<=']); } 
			if(isset($searchRule['_demands_headers.summ_pay>='])) { $whereArr['_demands_headers.summ_pay>='] = $searchRule['_demands_headers.summ_pay>=']; unset($searchRule['_demands_headers.summ_pay>=']); } 
			if(isset($searchRule['_tours.date_from<='])) { $whereArr['_tours.date_from<='] = $this->toMySQLDate($searchRule['_tours.date_from<=']); unset($searchRule['_tours.date_from<=']); } 
			if(isset($searchRule['_tours.date_to>='])) { $whereArr['_tours.date_to>='] = $this->toMySQLDate($searchRule['_tours.to>=']); unset($searchRule['_tours.date_to>=']); }
			if(isset($searchRule['_demandsstatuses.code'])) { $whereArr['_demandsstatuses.code'] = $this->toMySQLDate($searchRule['_demandsstatuses.code']); unset($searchRule['_demandsstatuses.code']); } 
			//var_dump($searchRule);
			if($whereArr)  $this->db->where($whereArr);
			$this->db->like($searchRule);
		}
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		
		if($constant_searchRule = $this->menucontainer->GetItemConstantSearchRule()){
			$this->db->where($constant_searchRule);
		}
		
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetDemandsstatusesList(){
		$this->db->select('_demandsstatuses.rid, demandsstatuses_name as demandsstatuses_name');
		$this->db->from('_demandsstatuses');
		$this->db->orderby('_demandsstatuses.d_order');
		$query = $this->DBGet('_demandsstatuses');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetCurrenciesList(){
		$this->db->select('_currencies.rid, _currencies.code as code');
		$this->db->from('_currencies');
		$this->db->orderby('code');
		$query = $this->DBGet('_currencies');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetFoodsList(){
		$this->db->select('_food.rid, _food.food_name as food_name');
		$this->db->from('_food');
		$this->db->orderby('food_name');
		$query = $this->DBGet('_food');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetRoomsList(){
		$this->db->select('_rooms.rid, _rooms.room_name as room_name');
		$this->db->from('_rooms');
		$this->db->orderby('room_name');
		$query = $this->DBGet('_rooms');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetCountriesList(){
		$this->db->select('_countries.rid, _countries.country_name as country_name');
		$this->db->from('_countries');
		$this->db->orderby('country_name');
		$query = $this->DBGet('_countries');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetCurourtsList(){
		$this->db->select('_curourts.rid, _curourts.curourt_name as curourt_name');
		$this->db->from('_curourts');
		$this->db->orderby('curourt_name');
		$query = $this->DBGet('_curourts');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetHotelscatsList(){
		$this->db->select('_hotelscats.rid, _hotelscats.hotelcat_name as hotelcat_name');
		$this->db->from('_hotelscats');
		$this->db->orderby('hotelcat_name');
		$query = $this->DBGet('_hotelscats');
		if($query->num_rows()>0) return $query->result();
		return False;
	}

	public function GetEditDS(){
		$this->db->select('_documents.rid, 
							DATE_FORMAT(_demands_headers.date_doc, \'%d.%m.%Y\') as date_doc, 
							_demands_headers._demandsstatuses_rid as _demandsstatuses_rid,
							_demandsstatuses.demandsstatuses_name as demandsstatuses_name,
							_demands_headers._currencies_rid as _currencies_rid,
							_demands_headers._calls_headers_rid as _calls_headers_rid,
							_demands_headers._tours_rid as _tours_rid,
							_demands_headers._advertisessources_rid as _advertisessources_rid, 
							_currencies.code as currency_code,
							_currencies.right_word as right_word,
							_demands_headers.agreement as agreement,
							_demands_headers.summ as summ,
							_demands_headers.summ_text as summ_text,
							_demands_headers.summ_pay as summ_pay,
							_demands_headers.curr_cource as curr_cource,
							_demands_headers.discount_per as discount_per,
							_demands_headers.discount_fix as discount_fix,
							_demands_headers.bank_s as bank_s,
							_demands_headers.touroperator_commision as touroperator_commision,
							_demands_headers.touroperator_koeff as touroperator_koeff,
							_demands_headers._zk_rid as _zk_rid,
							_demands_headers._zk_is_tourist as _zk_is_tourist,
							_tours._food_rid as _food_rid,
							_food.food_name as food_name,
							_tours._touroperators_rid as _touroperators_rid,
							_touroperators.touroperator_name as touroperator_name,
							_tours._rooms_rid as _rooms_rid,
							_tours.room_cat as room_cat,
							_rooms.room_name as room_name,
							_tours._countries_rid as _countries_rid,
							_countries.country_name as country_name,
							_tours._curourts_rid as _curourts_rid,
							_curourts.curourt_name as curourt_name,
							_tours._hotelscats_rid as _hotelscats_rid,
							_hotelscats.hotelcat_name as hotelcat_name,
							_tours.hotel_name as hotel_name,
							DATE_FORMAT(_tours.date_from, \'%d.%m.%Y\') as date_from,
							DATE_FORMAT(_tours.date_to, \'%d.%m.%Y\') as date_to,
							_tours.city_name as city_name,
							_tours.transfer as transfer,
							_tours.excursions as excursions,
							DATEDIFF(_tours.date_to, _tours.date_from) as tour_period,
							_demands_rows._clients_rid as _clients_rid,
							_clients.f_name as f_name,
							_clients.s_name as s_name,
							_clients.l_name as l_name,
							_clients.f_name_lat as f_name_lat,
							_clients.l_name_lat as l_name_lat,
							DATE_FORMAT(_clients.birthday, \'%d.%m.%Y\') as birthday,
							_clients.f_pass_seria as f_pass_seria,
							_clients.f_pass_num as f_pass_num,
							_clients.passp_seria as passp_seria,
							_clients.passp_num as passp_num,
							DATE_FORMAT(_clients.f_pass_period, \'%d.%m.%Y\') as f_pass_period,
							_clients._cities_rid as _cities_rid,
							_clients.adress as adress,
							_clients.phones as phones,
							_clients.email as email,
							_tours.order_summ as order_summ,
							_tours.order_image as order_image,
							_tours.order_name as order_name,
							_tours.order_type as order_type,
							_tours.order_size as order_size,
							_tours._currencies_rid as _tcurrencies_rid,
							_tours.summ_tour as summ_tour,
							_touroperators.touroperator_name as touroperator_name,
							_touroperators.stouroperator_name as stouroperator_name,
							_touroperators.license as license,
							_touroperators.contract as contract,
							_touroperators.contract_period as contract_period,
							DATE_FORMAT(_documents.modifyDT, \'%d.%m.%Y %H:%i\') as modifyDT, 
							_documents.descr as descr, _documents.archive
							');
		$this->db->from('_documents');
		$this->db->join('_demands_headers', '_demands_headers._documents_rid = _documents.rid');
		$this->db->join('_demands_rows', '_demands_rows._demands_headers_rid = _demands_headers.rid');
		$this->db->join('_demandsstatuses', '_demands_headers._demandsstatuses_rid = _demandsstatuses.rid');
		$this->db->join('_currencies', '_demands_headers._currencies_rid = _currencies.rid');
		$this->db->join('_tours', '_demands_headers._tours_rid = _tours.rid');
		$this->db->join('_food', '_tours._food_rid = _food.rid');
		$this->db->join('_touroperators', '_tours._touroperators_rid = _touroperators.rid');
		$this->db->join('_rooms', '_tours._rooms_rid = _rooms.rid');
		$this->db->join('_countries', '_tours._countries_rid = _countries.rid');
		$this->db->join('_curourts', '_tours._curourts_rid = _curourts.rid');
		$this->db->join('_hotelscats', '_tours._hotelscats_rid = _hotelscats.rid');
		$this->db->join('_clients', '_demands_rows._clients_rid = _clients.rid');
		//$this->db->join('_cities', '_clients._cities_rid = _cities.rid');
		$this->db->where(array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_documents');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function GetClientsDS($p_clients){
		$clientsArr = explode('-', $p_clients);
		if(!$clientsArr) return False;
		$this->db->select('	_clients.rid as _clients_rid,
							_clients.f_name as f_name,
							_clients.s_name as s_name,
							_clients.l_name as l_name,
							DATE_FORMAT(_clients.birthday, \'%d.%m.%Y\') as birthday,
							_clients.f_pass_seria as f_pass_seria,
							_clients.f_pass_num as f_pass_num,
							DATE_FORMAT(_clients.f_pass_period, \'%d.%m.%Y\') as f_pass_period');
		$this->db->from('_clients');
		$this->db->where(array('_clients.rid'=>$clientsArr[0])); unset($clientsArr[0]);
		foreach($clientsArr as $client) $this->db->orwhere(array('_clients.rid'=>$client));
		$query = $this->DBGet('_clients');
		if($query->num_rows()>0) return $query->result();
		return False;
	}
	
	public function CreateRecord(){
		$insDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->insert('_documents', $insDocumentArray);
		$docRid = $this->db->insert_id();
		$insToursArray = array('_touroperators_rid'=>$_POST['_touroperators_rid'],
								'_food_rid'=>$_POST['_food_rid'],
								'_rooms_rid'=>$_POST['_rooms_rid'],
								'room_cat'=>$_POST['room_cat'],
								'_countries_rid'=>$_POST['_countries_rid'],
								'_curourts_rid'=>$_POST['_curourts_rid'],
								'_currencies_rid'=>$_POST['_tcurrencies_rid'],
								'order_summ'=>$_POST['order_summ'],
								//'_cities_rid '=>$_POST['_cities_rid'],
								'_hotelscats_rid'=>$_POST['_hotelscats_rid'],
								'city_name'=>$_POST['city_name'],
								'hotel_name'=>$_POST['hotel_name'],
								'date_from'=>$this->toMySQLDate($_POST['date_from']),
								'date_to'=>$this->toMySQLDate($_POST['date_to']),
								'transfer'=>$_POST['transfer'],
								'excursions'=>$_POST['excursions'],
								'summ_tour'=>$_POST['summ_tour'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);
		// { Load image
		if($_FILES['order_image'] && $_FILES['order_image']['name']){
			$fileName = $_FILES['order_image']['name'];
			$tmpName  = $_FILES['order_image']['tmp_name'];
			$fileSize = $_FILES['order_image']['size'];
			$fileType = $_FILES['order_image']['type'];
			$fp      = fopen($tmpName, 'r');
			$content = fread($fp, filesize($tmpName));
			$content = $content;
			fclose($fp);
			$insToursArray['order_image'] = $content;
			$insToursArray['order_name'] = $fileName;
			$insToursArray['order_type'] = $fileType;
			$insToursArray['order_size'] = $fileSize;			
		}
		// } Load image
		$this->db->insert('_tours', $insToursArray);
		$tourRid = $this->db->insert_id();
		$q = $this->db->getwhere('_filials', array('rid'=>$this->ciObject->user->GetFilialRid()));
		$r = $q->row();
		$filialCode = $r->code;
		$insHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
								'_documents_rid'=>$docRid,
								'_demandsstatuses_rid'=>$_POST['_demandsstatuses_rid'],
								'_advertisessources_rid'=>$_POST['_advertisessources_rid'],
								'_tours_rid'=>$tourRid,
								'_currencies_rid'=>$_POST['_currencies_rid'],
								'touroperator_commision'=>$_POST['touroperator_commision'],
								'touroperator_koeff'=>$_POST['touroperator_koeff'],
								'_zk_rid'=>$_POST['_zk_rid'],
								'_zk_is_tourist'=>$_POST['_zk_is_tourist'],
								'agreement'=>$filialCode.'-'.date('Y/m/d').'-'.$docRid,
								'summ'=>$_POST['summ'],
								'summ_text'=>$_POST['summ_text'],
								'summ_pay'=>$_POST['summ_pay'],
								'curr_cource'=>$_POST['curr_cource'],
								'discount_per'=>$_POST['discount_per'],
								'discount_fix'=>$_POST['discount_fix'],
								'bank_s'=>$_POST['bank_s'],
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);
		$insHeaderArray['_calls_headers_rid'] = (!empty($_POST['_calls_headers_rid']))?$_POST['_calls_headers_rid']:null;
		$this->db->insert('_demands_headers', $insHeaderArray);
		$headerRid = $this->db->insert_id();
		foreach(explode('-',$_POST['_clients_rid']) as $clientRID){
			if($clientRID==$_POST['_zk_rid'] && $_POST['_zk_is_tourist']!='1') continue; 
			$insRowsArray = array('_demands_headers_rid'=>$headerRid,
									'_clients_rid'=>$clientRID,
									'archive'=>$_POST['archive'],
									'modifier_users_rid'=>$this->userRid,
									'modifyDT'=>$this->nowTime);
			$this->db->insert('_demands_rows', $insRowsArray);
		}
		$this->DemandFinOper($docRid);
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $docRid;
		}		
	}
	
	public function GetFilialDS(){
		$this->db->select('_filials._cities_rid, _cities.city_name, _filials.code, _filials.name,
							_filials.adress, _filials.phones, _filials.email');
		$this->db->from('_filials');
		$this->db->join('_cities', '_filials._cities_rid = _cities.rid');
		$this->db->where(array('_filials.rid'=>$this->ciObject->user->GetFilialRid()));
		$query = $this->db->get();
		return $query->row();
	}
	
	public function UpdateRecord(){
		$updateDocumentArray = array('_documents_types_rid'=>$this->typeDocRid, 
									'descr'=>$_POST['descr'],
									'modifyDT'=>$this->nowTime,
									'modifier_users_rid'=>$this->userRid);
		$this->db->trans_begin();
		$this->db->update('_documents', $updateDocumentArray, array('_documents.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$q = $this->db->getwhere('_demands_headers', array('_demands_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$r = $q->row();
		$tourRid = $r->_tours_rid;
		$updateToursArray = array('_touroperators_rid'=>$_POST['_touroperators_rid'],
								'_food_rid'=>$_POST['_food_rid'],
								'_rooms_rid'=>$_POST['_rooms_rid'],
								'room_cat'=>$_POST['room_cat'],
								'_countries_rid'=>$_POST['_countries_rid'],
								'_curourts_rid'=>$_POST['_curourts_rid'],
								'_currencies_rid'=>$_POST['_tcurrencies_rid'],
								'_hotelscats_rid'=>$_POST['_hotelscats_rid'],
								'order_summ'=>$_POST['order_summ'],
								'city_name'=>$_POST['city_name'],
								'hotel_name'=>$_POST['hotel_name'],
								'date_from'=>$this->toMySQLDate($_POST['date_from']),
								'date_to'=>$this->toMySQLDate($_POST['date_to']),
								'transfer'=>$_POST['transfer'],
								'excursions'=>$_POST['excursions'],
								'summ_tour'=>$_POST['summ_tour'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);
		// { Load image
		//var_dump($_FILE);
		if($_FILES['order_image'] && $_FILES['order_image']['name']){
			$fileName = $_FILES['order_image']['name'];
			$tmpName  = $_FILES['order_image']['tmp_name'];
			$fileSize = $_FILES['order_image']['size'];
			$fileType = $_FILES['order_image']['type'];
			$fp      = fopen($tmpName, 'r');
			$content = fread($fp, filesize($tmpName));
			$content = $content;
			fclose($fp);
			$updateToursArray['order_image'] = $content;
			$updateToursArray['order_name'] = $fileName;
			$updateToursArray['order_type'] = $fileType;
			$updateToursArray['order_size'] = $fileSize;			
		}
		// } Load image
		$this->db->update('_tours', $updateToursArray, array('_tours.rid'=>$tourRid));
		$updateHeaderArray = array('date_doc'=>$this->toMySQLDate($_POST['date_doc']),
								'_demandsstatuses_rid'=>$_POST['_demandsstatuses_rid'],
								'_advertisessources_rid'=>$_POST['_advertisessources_rid'],
								'_tours_rid'=>$tourRid,
								'_currencies_rid'=>$_POST['_currencies_rid'],
								'_zk_rid'=>$_POST['_zk_rid'],
								'_zk_is_tourist'=>$_POST['_zk_is_tourist'],
								'summ'=>$_POST['summ'],
								'summ_text'=>$_POST['summ_text'],
								'summ_pay'=>$_POST['summ_pay'],
								'curr_cource'=>$_POST['curr_cource'],
								'discount_per'=>$_POST['discount_per'],
								'discount_fix'=>$_POST['discount_fix'],
								'bank_s'=>$_POST['bank_s'],
								'touroperator_commision'=>$_POST['touroperator_commision'],
								'touroperator_koeff'=>$_POST['touroperator_koeff'],
								'archive'=>$_POST['archive'],
								'modifier_users_rid'=>$this->userRid,
								'modifyDT'=>$this->nowTime);
		$this->db->update('_demands_headers', $updateHeaderArray, array('_demands_headers._documents_rid'=>$this->ciObject->GetSessionData('activerecord')));
		$this->db->delete('_demands_rows', array('_demands_rows._demands_headers_rid'=>$r->rid));
		foreach(explode('-',$_POST['_clients_rid']) as $clientRID){
			if($clientRID==$_POST['_zk_rid'] && $_POST['_zk_is_tourist']!='1') continue;
			$insRowsArray = array('_demands_headers_rid'=>$r->rid,
									'_clients_rid'=>$clientRID,
									'archive'=>$_POST['archive'],
									'modifier_users_rid'=>$this->userRid,
									'modifyDT'=>$this->nowTime);
			$this->db->insert('_demands_rows', $insRowsArray);
		}
		$this->DemandFinOper($this->ciObject->GetSessionData('activerecord'));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $this->ciObject->GetSessionData('activerecord');
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_documents', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function GetOrderer($p_ordererRid){
		$query = $this->db->getwhere('_clients', array('rid'=>$p_ordererRid));
		return $query->row();	
	}
	
	public function GetTourCurrency(){
		$this->db->select('_currencies.code as currency_code');
		$this->db->from('_tours');
		$this->db->join('_currencies', '_currencies.rid = _tours._currencies_rid');
		$query = $this->db->get();
		if($query->num_rows()) return $query->row()->currency_code;
		return '';	
	}
	
	public function DemandFinOper($docRid){
		// Проводки по докменту
		$_filials_rid = $this->ciObject->user->GetFilialRid();
		if(!empty($_POST['summ_pay']) && $_POST['_demandsstatuses_rid'] == '12'){
			$this->ciObject->outgoingincoming_model->DelFinOper($docRid, '00003');
			$this->ciObject->outgoingincoming_model->DelFinOper($docRid, '00004');
			$this->ciObject->outgoingincoming_model->AddFinOper($docRid, date('Y-m-d'), '00003', $_POST['summ_pay'], $_POST['_currencies_rid'], $_filials_rid, 'P', 'Предоплата за тур');
		}
		if(!empty($_POST['summ_pay']) && $_POST['_demandsstatuses_rid'] == '13'){
			$this->ciObject->outgoingincoming_model->DelFinOper($docRid, '00004');
			$predoplata = $this->ciObject->outgoingincoming_model->GetDocFinOper($docRid, '00003');
			$summPR = 0;
			if($predoplata) $summPR = $predoplata[0]->summ_value;
			$this->ciObject->outgoingincoming_model->AddFinOper($docRid,  date('Y-m-d'), '00004', $_POST['summ_pay']-$summPR, $_POST['_currencies_rid'], $_filials_rid, 'P', 'Доплата за тур');
		}
	}
}
?>